<?php
/*
 * @Author: 李明
 * @Date: 2021-09-26 17:19:29
 * @LastEditTime: 2021-12-10 14:25:31
 * @LastEditors: 李明
 * @Description: 
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/app/product/ProductAppController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\app\product;

use App\Http\Controllers\common\CommonFun;
use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\ProductClass;
use Illuminate\Http\Request;

class ProductAppController extends Controller
{
    /**
     * @description: 商品分类列表(有树结构)
     * @param {Request} $request
     * @return {*}
     */
    public function ProductClassList(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }

        //接收前端传递过来的值
        $all = $request->all();
        $where = [];
        $where[] = ['app_status', '1'];
        $info = json_decode(
            ProductClass::where($where)
                ->orderby("create_time", "desc")
                ->get(),
            true
        );
        $info = CommonFun::ArrToTreeList($info, "id", "pid");
        return LMReturn::Success($info);
    }
    /**
     * @description: 产品列表
     * @param {Request} $request
     * @return {*}
     */
    public function ProductList(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm('user'), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        //设置where条件
        $where = [];
        $where[] = ['app_status', '1'];
        if (isset($all['class_id'])) {
            $where[] = ["product.class_id", $all['class_id']];
        }
        $size = isset($all['size']) ? $all['size'] : 15;
        $info = json_decode(
            json_encode(
                Product::select(
                    "product.id",
                    "product.name as name",
                    "product.long_time",
                    "product.time_unit",
                    "product.rate",
                    "product.money",
                    "product.ceiling",
                    "product.content",
                    "product.detail",
                    "product.sale_time",
                    "product.pay_type",
                    "product.pay_long",
                    "product.pay_time_unit",
                    "product.year_type",
                    "product.results_rate",
                    "product.state",
                    "product.app_status",
                    "product.wx_xcx_status",
                    "product.pc_status",
                    "product.remark",
                    "product_class.id as class_id",
                    "product_class.category_name as class_name"
                )
                    ->join("product_class", "product.class_id", "=", "product_class.id")
                    ->where($where)
                    ->orderby("product.update_time", "desc")
                    ->paginate($size)
            ),
            true
        );
        //将数字判断成汉字
        foreach ($info['data'] as $key => $value) {
            // 年化方式 1是年化比例 2是固定年化
            if ($value['year_type'] == 1) {
                $info['data'][$key]['year_type'] = '年化比例';
                $info['data'][$key]['year_type_rate'] = $value['results_rate'] . '%';
            } elseif ($value['year_type'] == 2) {
                $info['data'][$key]['year_type'] = '固定年化';
                $info['data'][$key]['year_type_rate'] = ($value['results_rate'] / 10000) . '万元';
            } else {
                $info['data'][$key]['year_type'] = '未知';
                $info['data'][$key]['year_type_rate'] = "设置错误";
            }

            // 产品周期 1日、 2月、3年
            if ($value['time_unit'] == 1) {
                $info['data'][$key]['product_time'] = $value['long_time'] . '日';
            } elseif ($value['time_unit'] == 2) {
                $info['data'][$key]['product_time'] = $value['long_time'] . '月';
            } elseif ($value['time_unit'] == 3) {
                $info['data'][$key]['product_time'] = $value['long_time'] . '年';
            } else {
                $info['data'][$key]['product_time'] = "设置错误";
            }
            if ($value['long_time'] == '') {
                $info['data'][$key]['product_time'] = '无期限';
            }
            // 付款方式付款方式  2自然月  4到期 5不付款 4自定义
            if ($value['pay_type'] == 5) {
                $info['data'][$key]['product_pay_time'] = '无需支付';
            } else {
                // 付款间隔 1日、 2月、3年
                if ($value['pay_time_unit'] == 1) {
                    $info['data'][$key]['product_pay_time'] = $value['pay_long'] . '日';
                } elseif ($value['pay_time_unit'] == 2) {
                    $info['data'][$key]['product_pay_time'] = $value['pay_long'] . '月';
                } elseif ($value['pay_time_unit'] == 3) {
                    $info['data'][$key]['product_pay_time'] = $value['pay_long'] . '年';
                } else {
                    $info['data'][$key]['product_pay_time'] = "设置错误";
                }
            }
        }


        return LMReturn::Success($info);
    }
}
